/*
 * Filename:    DrugStoreIntgrityServiceImpl.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-2-12
 */
package com.paic.mhis.hcpms.evaluation.evaluationDrugStore.drugStoreIllegal.biz.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.mhis.hcpms.evaluation.evaluationDrugStore.drugStoreIllegal.biz.service.DrugStoreIllegalService;
import com.paic.mhis.hcpms.evaluation.evaluationDrugStore.drugStoreIllegal.dao.DrugStoreIllegalDao;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvaluationOrgDTO;

@Service("drugStoreIllegalService")
public class DrugStoreIllegalServiceImpl implements DrugStoreIllegalService {

	@Autowired
	private DrugStoreIllegalDao drugStoreIllegalDao;

	/**
	 * 定点药店诚信List查询
	 */
	@Override
	public Map<String, Object> getDrugStoreList(Map<String, Object> map) {
		// Map<String, Object> map = new HashMap<String, Object>();
		List<String> strList = new ArrayList<String>();
		// 查询总数
		int orgCount = drugStoreIllegalDao.getDrugStoreListCount(map);
		List<EvaluationOrgDTO> orgList = null;
		if (orgCount > 0) {
			// 查询考评机构信息
			orgList = drugStoreIllegalDao.getDrugStoreList(map);
			for (EvaluationOrgDTO dto : orgList) {
				BigDecimal bedNum = dto.getBedNum();
				if (bedNum != null && bedNum.doubleValue() > 0) {
					dto.setBedName("有");
				} else {
					dto.setBedName("无");
				}
				String orgCode = dto.getOrgCode();
				String year = dto.getEvaluationYear();
				map.put("orgCode", orgCode);
				map.put("year", year);
				// 考评得分查询
				strList = drugStoreIllegalDao.getEvaluationScore(map);
				int i = 0;
				for (String str : strList) {
					if (i == 0) {
						dto.setOne(str);
					}
					if (i == 1) {
						dto.setTwo(str);
					}
					if (i == 2) {
						dto.setThree(str);
					}
					if (i == 3) {
						dto.setFour(str);
					}
					if (i == 4) {
						dto.setFive(str);
					}
					if (i == 5) {
						dto.setSix(str);
					}
					i++;
				}
			}
		}
		if (null == orgList) {
			orgList = new ArrayList<EvaluationOrgDTO>();
		}
		map.put("orgList", orgList);
		map.put("orgCount", orgCount);
		return map;
	}

	/**
	 * 根据年份查询当前需要展示的考评得分名称集合
	 * 
	 * @return
	 */
	@Override
	public List<String> getEvaluationName(String year, String type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("year", year);
		map.put("type", type);
		return drugStoreIllegalDao.getEvaluationName(map);
	}
	/**
	 * 药店违规
	 */
	@Override
	public Map<String, Object> drugStoreIllegal(
			Map<String, Object> map) {
		
		Map<String,Object>  resultMap=new HashMap<String,Object>();
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		//查询预警次数以及涉及金额
		Map<String,Object> warnData=drugStoreIllegalDao.queryEvaluationDrugStoreIllegalWarn(map).get(0);
		//提醒次数
		map.put("process","01");
		Map<String,Object> map01=drugStoreIllegalDao.queryEvaluationDrugStoreIllegalWarn(map).get(0);
		map01.put("code", "01");
		map01.put("name","提醒");
		list.add(map01);
		//书面预警次数
		map.put("process", "02");
		Map<String,Object> map02=drugStoreIllegalDao.queryEvaluationDrugStoreIllegalWarn(map).get(0);
		map02.put("code", "02");
		map02.put("name","书面预警");
		list.add(map02);
		//约谈预警次数interviewWarnNum
		map.put("process", "03");
		Map<String,Object> map03=drugStoreIllegalDao.queryEvaluationDrugStoreIllegalWarn(map).get(0);
		map03.put("code","03");
		map03.put("name","约谈预警");
		list.add(map03);
		//转稽核次数toAuditNum
		map.put("process","04");
		Map<String,Object> map04=drugStoreIllegalDao.queryEvaluationDrugStoreIllegalWarn(map).get(0);
		map04.put("code","04");
		map04.put("name","转稽核");
		list.add(map04);
		//其它次数otherNum
		map.put("process", "09");
		Map<String,Object> map09=drugStoreIllegalDao.queryEvaluationDrugStoreIllegalWarn(map).get(0);
		map09.put("code", "09");
		map09.put("name","其它");
		list.add(map09);
		warnData.put("list",list);
		//预警
		resultMap.put("warnData",warnData);
		//稽核
		resultMap.put("auditData",drugStoreIllegalDao.queryEvaluationDrugStoreIllegalAudit(map).get(0));
		//
		Map<String,Object> illegalWorkers=new HashMap<String,Object>();
		resultMap.put("illegalWorkers",illegalWorkers);
		return resultMap;
	}

	/**
	 * 药店违规预警详情
	 */
	@Override
	public Map<String, Object> drugStoreIllegalWarnDetails(
			Map<String, Object> map) {
		Map<String,Object>  resultMap=new HashMap<String,Object>();
		resultMap.put("tableData",drugStoreIllegalDao.drugStoreIllegalWarnDetails(map));
		return resultMap;
	}
	/**
	 * 药店违规稽核详情
	 */
	@Override
	public Map<String, Object> drugStoreIllegalAuditDetails(
			Map<String, Object> map) {
		Map<String,Object>  resultMap=new HashMap<String,Object>();
		resultMap.put("tableData",drugStoreIllegalDao.drugStoreIllegalAuditDetails(map));
		return resultMap;
	}
	/**
	 * 导出药店违规情况
	 */
	@Override
	public void exportDrugStoreIllegal(HttpServletRequest request,HttpServletResponse response,Map<String, Object> map){
		
		StringBuffer wr=new StringBuffer(1024);
		Map<String,Object> warnResultMap=drugStoreIllegal(map);
		@SuppressWarnings("unchecked")
		Map<String,Object> warnData=(Map<String, Object>) warnResultMap.get("warnData");
		@SuppressWarnings("unchecked")
		Map<String,Object> auditData=(Map<String,Object>) warnResultMap.get("auditData");
		wr.append(map.get("orgName")+"违规情况:,");
		wr.append("\r\n");
		wr.append("\r\n");
		wr.append(",1、预警次数：,,"+warnData.get("num")+"次,,");
		wr.append("涉及金额:,");
		wr.append(warnData.get("money")+",");
		wr.append("元,");
		wr.append("\r\n");
		//预警次数结束
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> list=(List<Map<String, Object>>) warnData.get("list");
		for(int i=0;i<list.size();i++)
		{
			HashMap<String,Object> tempMap=(HashMap<String, Object>) list.get(i);
			wr.append(",");
			if(i==0)
				{wr.append("其中:"+",");}
			else
				{wr.append(",");}
			wr.append(tempMap.get("name")+":,");
			wr.append(tempMap.get("num")+"次,");
			wr.append(",");
			wr.append("涉及金额:,");
			wr.append(tempMap.get("money")+",");
			wr.append("元,");
			wr.append("\r\n");
		}
		wr.append("\r\n");
		//预警结束
		wr.append(",");
		wr.append("2、稽核次数:,");
		wr.append(",");
		wr.append(auditData.get("num")+"次,");
		wr.append(",");
		wr.append("涉及金额：,");
		wr.append(auditData.get("money")+",");
		wr.append("元,");
		wr.append("\r\n");
		wr.append("\r\n");
		//稽核结束
		wr.append(",");
		wr.append("3、其中一次记十二分:,");
		wr.append(",");
		wr.append("(填充数据)"+"次,");
		wr.append("\r\n");
		wr.append("\r\n");
		//12分结束
		wr.append(",");
		wr.append("4、FWA事后审核情况:"+",");
		wr.append("\r\n");
		//1
		wr.append(",");
		wr.append(",");
		wr.append("单据数:"+",");
		wr.append("0条"+",");
		wr.append(",");
		wr.append("涉及金额:"+",");
		
		wr.append("0,元"+",");
		wr.append("\r\n");
		//first row end 
		wr.append(",");
		wr.append("其中:,");
		wr.append("可疑:"+",");
		wr.append("0条"+",");
		wr.append(",");
		wr.append("涉及金额:"+",");
		
		wr.append("0,元"+",");
		wr.append("\r\n");
		//two end
		wr.append(",");
		wr.append(",");
		wr.append("高度可疑:"+",");
		wr.append("0条"+",");
		wr.append(",");
		wr.append("涉及金额:"+",");
		
		wr.append("0,元"+",");
		wr.append("\r\n");
		//three end
		wr.append(",");
		wr.append(",");
		wr.append("违规:"+",");
		wr.append("0条"+",");
		wr.append(",");
		wr.append("涉及金额:"+",");
		
		wr.append("0,元"+",");
		wr.append("\r\n");
		
		response.reset();
		response.addHeader("Pragma", "no-cache");
		response.addHeader("Cache-Control", "no-cache");
		response.setContentType("APPLICATION/OCTET-STREAM");
		String name=map.get("orgName").toString()+"违规情况";
		String fileName;
		try{
		fileName = new String(name.getBytes("utf-8"), "ISO8859-1")+".csv";
		response.setHeader("content-disposition", "attachment;fileName=\"" + fileName + "\"");
		OutputStream os=response.getOutputStream();
		byte[] buffer = wr.toString().getBytes("gbk");
		os.write(buffer);
		os.flush();
		os.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
